package com.open.capacity.user.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Dictionary;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.dao.DictionaryDao;
import com.open.capacity.user.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    private DictionaryDao dictionaryDao;


    @Override
    public PageResult<Dictionary> getSelectDictionaries(Map<String, Object> params) {
        try {
            int total = dictionaryDao.count(params);
            List<Dictionary> list = Collections.emptyList();
            if (total > 0) {
                PageUtil.pageParamConver(params, true);
                list = dictionaryDao.findList(params);//查询所有用户信息

            }
            return PageResult.<Dictionary>builder().data(list).code(0).count((long) total).build();//收集所有数据
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result addDictionary(Dictionary dictionary) {
        boolean isHave = isHaveDccode(dictionary.getDccode(),dictionary.getDctype());
        if (!isHave){
            dictionary.setEnabled(Boolean.TRUE);
            int result = dictionaryDao.addDictionary(dictionary);
            return Result.succeed(result == 1?"添加成功":"添加失败");
        }else{
            return Result.failed("该字典代码和字典类型不能同时重复");
        }
    }

    /**
     * 判断字典Code和字典类型是否同时存在
     * @param dccode
     * @param dctype
     * @return
     */
    private boolean isHaveDccode(String dccode, String dctype) {
        int isHave = dictionaryDao.isHaveDccode(dccode,dctype);
        return isHave > 0;
    }


    @Override
    public Result updateDictionaryEnabled(Map<String, Object> params) {
        try {
            Long id = MapUtils.getLong(params, "id");
            Boolean enabled = MapUtils.getBoolean(params, "enabled");

            Dictionary dictionary = dictionaryDao.findById(id);
            if (dictionary == null) {
                return Result.failed("字典内容不存在");
                //throw new IllegalArgumentException("用户不存在");
            }
            dictionary.setEnabled(enabled);

            int i = dictionaryDao.updateDictionaryEnabled(dictionary);
            log.info("修改字典：{}", dictionary);

            return i > 0 ? Result.succeed(dictionary, "更新成功") : Result.failed("更新失败");
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    public Result updateDictionary(Dictionary dictionary) {
//        boolean isHave = isHaveDccode(dictionary.getDccode(),dictionary.getDctype());
//        if (isHave){
//            return Result.failed("该字典类型已存在");
//        }
        int result = dictionaryDao.updateDictionary(dictionary);
        return Result.succeed(result == 1 ? "修改成功" : "修改失败");
    }


}
